package com.huans.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.huans.model.User;

/**
 * User DAO
 * 
 * @author lich
 * 
 */
public class UserDao {
	private DataSource dataSource;

	public User get(String userId) {
		User user = null;
		String sql = "select * from t_user t where t.userId = ? ";
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = dataSource.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User();
				user.setUserId(rs.getString("userId"));
				user.setUserName(rs.getString("userName"));
				user.setGender(rs.getString("gender"));
				user.setCreateDate(rs.getTimestamp("createDate"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return user;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
}
